package Servises;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import model.UrlFile;

import org.apache.http.util.ByteArrayBuffer;

import android.os.Environment;

public class FilesManager {
	private FilesManager() {
		// this is a singleton...
	}

	public static String SaveTempFileFromURL(UrlFile url) throws IOException {
		return SaveTempFileFromURL(url.getUrl(), url.getFileName(), url
				.getFileSuffix(), null, true);
	}
	
	public static String SaveTempFileFromURL(String urlStr, boolean useCahce) throws IOException {
		UrlFile url = new UrlFile(urlStr); 
		return SaveTempFileFromURL(url.getUrl(), url.getFileName(), url
				.getFileSuffix(), null, useCahce);
	}
	
	public static String SaveTempFileFromURL(String urlStr) throws IOException {
		UrlFile url = new UrlFile(urlStr); 
		return SaveTempFileFromURL(url.getUrl(), url.getFileName(), url
				.getFileSuffix(), null, true);
	}

	public static String SaveTempFileFromURL(String urlPath, String fileName,
			String fileSuffix, String directory, boolean useCahce) throws IOException {
		File temp;
		if (directory == null)
			directory = "mikizaza";
			
		directory = String.format("/%s/%s/", Environment.getExternalStorageDirectory(), directory);
		
		temp = new File(directory,fileName+"."+fileSuffix);
		String tempPath = temp.getAbsolutePath();

		//Create all the dirs in its path
		temp.getParentFile().mkdirs();
		
		//If file already exists do nothing			
		if (!tempPath.endsWith("xml")&& useCahce && (temp.exists() || !temp.createNewFile()))
			return tempPath;
		
		URL url = new URL(urlPath);
		URLConnection cn = url.openConnection();
		cn.connect();
		InputStream stream = cn.getInputStream();
		if (stream == null)
			throw new RuntimeException("stream is null");
		

		FileOutputStream out = new FileOutputStream(temp);
		byte buf[] = new byte[128];
		do {
			int numread = stream.read(buf);
			if (numread <= 0)
				break;
			out.write(buf, 0, numread);
		} while (true);

		stream.close();
		out.close();

		return tempPath;
	}

	public static void RemoveFile(String path) {
		new File(path).delete();
	}

	public static byte[] getFileData(String urlPath) throws Exception {

		URL url = new URL(urlPath);
		URLConnection cn = url.openConnection();
		cn.connect();
		InputStream stream = cn.getInputStream();
		if (stream == null)
			throw new RuntimeException("stream is null");

		ByteArrayBuffer baf = new ByteArrayBuffer(50);
		byte buf[] = new byte[128];
		do {
			int numread = stream.read(buf);
			if (numread <= 0)
				break;
			baf.append(buf, 0, numread);

		} while (true);

		stream.close();

		return baf.toByteArray();
	}

	public static byte[] getFileData1(String url) throws Exception {
		/* Define the URL we want to load data from. */
		URL myURL = new URL(url);
		/* Open a connection to that URL. */
		URLConnection ucon = myURL.openConnection();

		/*
		 * Define InputStreams to read from the URLConnection.
		 */
		InputStream is = ucon.getInputStream();
		BufferedInputStream bis = new BufferedInputStream(is);

		/*
		 * Read bytes to the Buffer until there is nothing more to read(-1).
		 */
		ByteArrayBuffer baf = new ByteArrayBuffer(50);
		int current = 0;
		while ((current = bis.read()) != -1) {
			baf.append((byte) current);
		}

		/* Convert the Bytes read to a String. */
		return baf.toByteArray();
	}

}
